﻿@using System.Web.Script.Serialization
@{
    ViewBag.Title = T("Login_Title");
}

<div class="container">
    <form data-bind="enterkey: Login">
        <fieldset>
            <legend>@T("Login_Title")</legend>

            <p class="text-error" data-bind="visible:ErrorMessage != null,text:ErrorMessage"></p>

            <div class="form-group">
                <label for="">@T("Login_Email")</label>
                <input type="text" class="form-control" data-bind="value:Email,attr:{placeholder:'@T("Login_Email")'}">
                <p data-bind="validationMessage: Email" class="text-error"></p>
            </div>

            <div class="form-group">
                <label for="">@T("Login_Password")</label>
                <input type="password" class="form-control" data-bind="value:Password,attr:{placeholder:'@T("Login_Password")'}">
                <p data-bind="validationMessage: Password" class="text-error"></p>
            </div>



            <input type="button" id="submitBtn" data-bind="value:'@T("Login_Login")', click:Login" class="btn btn-primary"/>

        </fieldset>
    </form>
</div>
@{
    var model = ViewBag.Model;

    var jsonStr = new HtmlString(new JavaScriptSerializer().Serialize(model));
}

@section scripts{

    <script>

     
        var init = @jsonStr;


        
        function LoginModel() {
            var self = this;
            self.Email = ko.observable(init.Email).extend({
                required: { message: '@T("Login.NeedEmailAddress")' },
            });
            self.Password = ko.observable(init.Password).extend({
                required: { message: '@T("Login.NeedPassword")'},
            });

            self.ErrorMessage = ko.observable(null);

           
            
            self.Login = function() {
                self.ErrorMessage(null);
                
                if (this.isValid()) {
                    $.ajax({
                        url: "/api/LoginApi/Login",
                        type: 'post',
                        data: ko.toJSON(self),
                        contentType: 'application/json',
                        success: function(result) {

                            if (result.Status == "0") {
                                location.href = result.RedirectUrl;
                            } else {
                                self.ErrorMessage(result.Message);
                            }
                        }
                    });

                } else {

                    self.errors.showAllMessages();
                }
            };
        }

        var vm = new LoginModel();
        vm.errors = ko.validation.group(vm);
        ko.applyBindings(vm);
    </script>
}
